package com.edu.zhku.wms.controller;

import com.edu.zhku.wms.common.CommonResult;
import com.edu.zhku.wms.common.Update;
import com.edu.zhku.wms.dto.WageQueryDto;
import com.edu.zhku.wms.pojo.Wage;
import com.edu.zhku.wms.service.WageServiceImpl;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Api(tags = "工资管理")
@RestController
@RequestMapping("/api/wage")
public class WageController {

    @Autowired
    WageServiceImpl wageService;

    @ApiOperation("添加工资信息")
    @Secured({"管理员", "财务"})
    @PostMapping("/addWage")
    public CommonResult addWage(@RequestBody @Validated Wage wage) {
        if (wageService.addWage(wage) > 0) {
            return CommonResult.success();
        }
        return CommonResult.error("添加工资信息失败");
    }

    @ApiOperation("删除工资信息")
    @Secured({"管理员", "财务"})
    @DeleteMapping("/deleteWage")
    public CommonResult deleteWage(Integer id) {
        if (wageService.deleteWageById(id) > 0) {
            return CommonResult.success();
        }
        return CommonResult.error("删除工资信息失败");
    }

    @ApiOperation("修改工资信息（工资所属员工、年月不可修改，请移除并添加）")
    @Secured({"管理员", "财务"})
    @PutMapping("/updateWage")
    public CommonResult updateWage(@RequestBody @Validated(Update.class) Wage wage) {
        if (wageService.updateWage(wage) > 0) {
            return CommonResult.success();
        }
        return CommonResult.error("修改工资信息失败");

    }

    @ApiOperation("获取工资列表（可指定员工和年份进行筛选）")
    @Secured({"管理员", "财务"})
    @GetMapping("/wageList")
    public CommonResult wageList(WageQueryDto wageQueryDto,
                                 @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                 @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        return CommonResult.success(wageService.wageList(wageQueryDto, pageNum, pageSize));
    }

    @ApiOperation("获取目前所登录的用户所绑定的员工的工资列表")
    @Secured({"普通用户", "财务"})
    @GetMapping("/myWageList")
    public CommonResult myWageList(HttpServletRequest request,
                                   Integer year,
                                   @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                   @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        return CommonResult.success(wageService.getCurrentWageList(request, year, pageNum, pageSize));
    }
}
